package com.gree.flowable.app.listener.task;

import com.gree.flowable.app.listener.BaseListener;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component(value = "setAssigneeListener")
public class SetAssigneeListener extends BaseListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        if (delegateTask.getAssignee() == null) {
            // 分配处理人
            Object assignees = delegateTask.getVariable(delegateTask.getTaskDefinitionKey() + "_assignee");
            if (assignees == null || assignees.toString().length() == 0){
                throw new RuntimeException("没有设置候选人！");
            }
            List<String> users = new ArrayList<>();
            String[] assigneesArray = assignees.toString().split(",");
            for (String as : assigneesArray){
                users.add(as);
            }

            delegateTask.addCandidateUsers(users);
        }
    }
}
